<?php
namespace app\admin\model;
use frame\libs\BaseModel;

/**
 * 
 */
class NoticeModel extends BaseModel
{
    protected $table = "system_notice"; 

    /**
     * 获取所有公告信息
     * @return [type] [description]
     */
    public function getNotices()
    {
        return $this->fetchAll("SELECT * FROM $this->table WHERE is_deleted=0 ORDER BY is_top DESC,sort DESC");
    }

    /**
     * 后台首页获取所有未过期公告信息
     * @return [type] [description]
     */
    public function getNoticesLimit()
    {
        $now = time();
        return $this->fetchAll("SELECT * FROM $this->table WHERE is_deleted=0 AND expiration_time>=$now ORDER BY is_top DESC,sort DESC LIMIT 12");
    }

    /**
     * 根据id获取公告
     * @param [type] $id [description]
     */
    public function getNoticeById($id)
    {
        return $this->fetchOne("SELECT * FROM $this->table WHERE id=:id", array('id'=>$id));
    }

    /**
     * 新增公告
     * @param  array $data [description]
     * @return bool       [description]
     */
    public function insertNotice($data)
    {
        try {
            $this->beginTransaction();
            $this->insert($data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新公告
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function updateNotice($data)
    {
        try {
            $this->beginTransaction();
            $sql = "UPDATE $this->table SET title=:title,content=:content,tag=:tag,is_top=:is_top,expiration_time=:expiration_time,update_time=:update_time,update_uid=:update_uid WHERE id=:id";
            $this->update($sql, $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 删除
     * @param  int $id [description]
     * @return bool     [description]
     */
    public function deleteNoticeById($data)
    {
        try {
            $this->beginTransaction();
            $this->update("UPDATE $this->table SET is_deleted=1,update_uid=:update_uid,update_time=:update_time WHERE id=:id", $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新置顶
     * @param  int $id     [description]
     * @param  int $status [description]
     * @return bool         [description]
     */
    public function updateIsTop($data)
    {   
        try {
            $this->beginTransaction();
            $this->update("UPDATE $this->table SET is_top=:is_top,update_time=:update_time,update_uid=:update_uid WHERE id=:id", $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新排序
     * @param  int $id     [description]
     * @param  int $status [description]
     * @return bool         [description]
     */
    public function updateSort($data)
    {   
        try {
            $this->beginTransaction();
            $this->update("UPDATE $this->table t1 JOIN $this->table t2 ON (t1.id=:id1 AND t2.id=:id2) SET t1.sort = t2.sort,t2.sort=t1.sort,t1.update_time=:update_time,t2.update_time=:update_time,t1.update_uid=:update_uid,t2.update_uid=:update_uid", $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

}

